package com.netease.financial.common.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.financial.common.util.preferences.DebugPreferencesHelper;

/**
 * Created by wyf on 7/7/15.
 */
public class DialogUtil {

    public static void showDialog(Context context, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(message);
        builder.create().show();
    }

    public static void showDebugDialog(final Context context) {
        LinearLayout serverLayout = new LinearLayout(context);
        final EditText ipEditText = new EditText(context);
        String IP = DebugPreferencesHelper.getDomainBody();
        String PORT = DebugPreferencesHelper.getDomainPort();
        ipEditText.setText(IP);

        boolean isDebugMode = DebugPreferencesHelper.isDebugMode();
        final CheckBox debugCheckBox = new CheckBox(context);
        debugCheckBox.setText("开启debug开关");
        debugCheckBox.setChecked(isDebugMode);
        debugCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DebugPreferencesHelper.setToastDebugOpen(isChecked);
            }
        });

        TextView serverTitle = new TextView(context);
        serverTitle.setText("设置服务器ip地址和端口\n确定后退出应用并重新进入生效");
        final EditText portEditText = new EditText(context);
        portEditText.setText(PORT);
        serverLayout.addView(ipEditText);
        serverLayout.addView(portEditText);

        TextView debugTitle = new TextView(context);
        debugTitle.setText("设置debug开关模式");
        LinearLayout view = new LinearLayout(context);
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(serverTitle);
        view.addView(serverLayout);

        view.addView(debugTitle);
        view.addView(debugCheckBox);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("设置").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DebugPreferencesHelper.setDomainBody(ipEditText.getText().toString());
                DebugPreferencesHelper.setDomainPort(portEditText.getText().toString());
                DebugPreferencesHelper.setDebugMode(debugCheckBox.isChecked());
            }
        }).setView(view).create().show();
    }

}
